/**
 * Copyright (c) 2012-2013 YunZhiSheng(Shanghai) Co.Ltd. All right reserved.
 * @FileName : RomControl.java
 * @ProjectName : vui_assistant
 * @PakageName : cn.yunzhisheng.vui.assistant.oem
 * @Author : Dancindream
 * @CreateDate : 2013-9-6
 */
package com.zl.api;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;

//import cn.yunzhisheng.vui.assistant.database.NAVILocationProvider;
//import cn.yunzhisheng.vui.assistant.gaode.PoiKeywordSearchActivity;

/**
 * @Module : 隶属模块名
 * @Comments : 描述
 * @Author : Dancindream
 * @CreateDate : 2013-9-6
 * @ModifiedBy : Dancindream
 * @ModifiedDate: 2013-9-6
 * @Modified:
 * 2013-9-6: 实现基本功能
 */
public class RomControl {
	public static final String TAG = "RomControl";
	public static final String ROM_APP_LAUNCH = "ROM_APP_LAUNCH";
	public static final String ROM_APP_UNINSTALL = "ROM_APP_UNINSTALL";
	public static final String ROM_APP_EXIT = "ROM_APP_EXIT";
	public static final String ROM_CALL = "ROM_CALL";
	public static final String ROM_BROWSER_URL = "ROM_BROWSER_URL";
	public static final String ROM_SEND_SMS = "ROM_SEND_SMS";

	public static final String ROM_OPEN_3G = "ROM_OPEN_3G";
	public static final String ROM_CLOSE_3G = "ROM_CLOSE_3G";
	public static final String ROM_OPEN_DISPLAY_SETTINGS = "ROM_OPEN_DISPLAY_SETTINGS";
	public static final String ROM_OPEN_BLUETOOTH = "ROM_OPEN_BLUETOOTH";
	public static final String ROM_CLOSE_BLUETOOTH = "ROM_CLOSE_BLUETOOTH";
	public static final String ROM_OPEN_TIME_SETTINGS = "ROM_OPEN_TIME_SETTINGS";
	public static final String ROM_OPEN_MODEL_INAIR = "ROM_OPEN_MODEL_INAIR";
	public static final String ROM_CLOSE_MODEL_INAIR = "ROM_CLOSE_MODEL_INAIR";
	public static final String ROM_OPEN_MODEL_MUTE = "ROM_OPEN_MODEL_MUTE";
	public static final String ROM_CLOSE_MODEL_MUTE = "ROM_CLOSE_MODEL_MUTE";
	public static final String ROM_OPEN_MODEL_VIBRA = "ROM_OPEN_MODEL_VIBRA";
	public static final String ROM_CLOSE_MODEL_VIBRA = "ROM_CLOSE_MODEL_VIBRA";
	public static final String ROM_OPEN_SOUND_SETTINGS = "ROM_OPEN_SOUND_SETTINGS";
	public static final String ROM_OPEN_ROTATION = "ROM_OPEN_ROTATION";
	public static final String ROM_CLOSE_ROTATION = "ROM_CLOSE_ROTATION";
	public static final String ROM_OPEN_WALLPAPER_SETTINGS = "ROM_OPEN_WALLPAPER_SETTINGS";
	public static final String ROM_OPEN_WIFI = "ROM_OPEN_WIFI";
	public static final String ROM_CLOSE_WIFI = "ROM_CLOSE_WIFI";
	public static final String ROM_OPEN_WIFI_SPOT = "ROM_OPEN_WIFI_SPOT";
	public static final String ROM_CLOSE_WIFI_SPOT = "ROM_CLOSE_WIFI_SPOT";

	public static final String ROM_INCREASE_VOLUMNE = "ROM_INCREASE_VOLUMNE";
	public static final String ROM_DECREASE_VOLUMNE = "ROM_DECREASE_VOLUMNE";
	public static final String ROM_VOLUMNE_MAX = "ROM_VOLUMNE_MAX";
	public static final String ROM_VOLUMNE_MIN = "ROM_VOLUMNE_MIN";
	public static final String ROM_VOLUMNE_SET = "ROM_VOLUMNE_SET";

	public static final String START_ASSISTANT = "START_ASSISTANT";

	public static final String OEM_VIDEO_SHOW = "OEM_VIDEO_SHOW";
	
	public static final String ROM_OPEN_SCREEN = "ROM_OPEN_SCREEN";
	public static final String ROM_CLOSE_SCREEN = "ROM_CLOSE_SCREEN";
	public static final String ROM_OPEN_CARCORDER = "ROM_OPEN_CARCORDER";
	public static final String ROM_OPEN_NAVI = "ROM_OPEN_NAVI";
	public static final String ROM_CLOSE_NAVI = "ROM_CLOSE_NAVI";
	public static final String ROM_NAVI_HOME = "ROM_NAVI_HOME";
	public static final String ROM_NAVI_COMPANY = "ROM_NAVI_COMPANY";
	public static final String ROM_OPEN_RADIO = "ROM_OPEN_FM";
	public static final String ROM_CLOSE_RADIO = "ROM_CLOSE_FM";
	public static String BACK_MAIN_PAGE = "BACK_MAIN_PAGE";
	public static final String ROM_OPEN_CAMERA = "ROM_OPEN_CAMERA";
	public static final String GO_HOME = "home";
	public static final String GO_COMPANY = "company";
	private static final String NAVIGATION_KEY = "navi_gation_default";
	private static Context mContext;
	private static String packages = "";
	private static final String XMGD_NAVI_PACKAGE = "com.autonavi.xmgd.navigator"; 

	public static void enterControl(Context context, String type, Object... params) {
		mContext = context;
		String logParams = "";
		if (params != null && params.length > 0) {
			for (Object obj : params) {
				logParams += "; p:" + String.valueOf(obj);
			}
		}
		Log.d(TAG, "TYPE:" + type + logParams);

		if (ROM_APP_LAUNCH.equals(type)) {
			RomApp.lanchApp(context, (String) params[0], (String) params[1]);
		} else if (ROM_APP_UNINSTALL.equals(type)) {
			RomApp.uninstallApp(context, (String) params[0]);
		} else if (ROM_OPEN_3G.equals(type)) {
			RomNetWork.setMobileDataStatus(context, true);
		} else if (ROM_CLOSE_3G.equals(type)) {
			RomNetWork.setMobileDataStatus(context, false);
		} else if (ROM_OPEN_DISPLAY_SETTINGS.equals(type)) {
			RomSystemSetting.openDisplaySettings(context);
		} else if (ROM_OPEN_BLUETOOTH.equals(type)) {
			RomSystemSetting.setBluetoothEnabled(true);
		} else if (ROM_CLOSE_BLUETOOTH.equals(type)) {
			RomSystemSetting.setBluetoothEnabled(false);
		} else if (ROM_OPEN_TIME_SETTINGS.equals(type)) {
			RomSystemSetting.openTimeSettings(context);
		} else if (ROM_OPEN_MODEL_INAIR.equals(type)) {
			RomSystemSetting.setFlightModeEnabled(context, true);
		} else if (ROM_CLOSE_MODEL_INAIR.equals(type)) {
			RomSystemSetting.setFlightModeEnabled(context, false);
		} else if (ROM_OPEN_MODEL_MUTE.equals(type)) {
			RomSystemSetting.setRingerMode(context, true, false);
		} else if (ROM_CLOSE_MODEL_MUTE.equals(type)) {
			RomSystemSetting.setRingerMode(context, false, false);
		} else if (ROM_OPEN_MODEL_VIBRA.equals(type)) {
			RomSystemSetting.setRingerMode(context, false, true);
		} else if (ROM_CLOSE_MODEL_VIBRA.equals(type)) {
			RomSystemSetting.setRingerMode(context, false, false);
		} else if (ROM_OPEN_SOUND_SETTINGS.equals(type)) {
			RomSystemSetting.openSoundSettings(context);
		} else if (ROM_OPEN_ROTATION.equals(type)) {
			RomSystemSetting.setAutoOrientationEnabled(context, true);
		} else if (ROM_CLOSE_ROTATION.equals(type)) {
			RomSystemSetting.setAutoOrientationEnabled(context, false);
		} else if (ROM_OPEN_WALLPAPER_SETTINGS.equals(type)) {
			RomSystemSetting.openWallPaperSettings(context);
		} else if (ROM_OPEN_WIFI.equals(type)) {
			RomNetWork.setWifiEnabled(context, true);
		} else if (ROM_CLOSE_WIFI.equals(type)) {
			RomNetWork.setWifiEnabled(context, false);
		} else if (ROM_OPEN_WIFI_SPOT.equals(type)) {
			RomNetWork.setWifiSpotEnabled(context, true);
		} else if (ROM_CLOSE_WIFI_SPOT.equals(type)) {
			RomNetWork.setWifiSpotEnabled(context, false);
		} else if (ROM_BROWSER_URL.equals(type)) {
			RomSystemSetting.openUrl(context, (String) params[0]);
		} else if (ROM_DECREASE_VOLUMNE.equals(type)) {
			RomSystemSetting.RaiseOrLowerVolume(context, false, (Integer) params[0]);
		} else if (ROM_INCREASE_VOLUMNE.equals(type)) {
			RomSystemSetting.RaiseOrLowerVolume(context, true, (Integer) params[0]);
		} else if (ROM_VOLUMNE_MAX.equals(type)) {
			RomSystemSetting.setMaxVolume(context);
		} else if (ROM_VOLUMNE_MIN.equals(type)) {
			RomSystemSetting.setMinVolume(context);
		} else if (ROM_VOLUMNE_SET.equals(type)) {
			RomSystemSetting.setVolume(context, (Integer) params[0]);
		} else if (START_ASSISTANT.equals(type)) {

		} else if (OEM_VIDEO_SHOW.equals(type)) {

		} else if (ROM_APP_EXIT.equals(type)) {
			RomApp.exitApp(context, (String) params[0], (String) params[1]);
		} else if (ROM_OPEN_SCREEN.equals(type)) { //打开屏幕
			RomSystemSetting.OpenScreen(context);
		} else if (ROM_CLOSE_SCREEN.equals(type)) {  //关闭屏幕
			RomSystemSetting.CloseScreen(context);
		} else if (ROM_OPEN_CARCORDER.equals(type)) {  //打开行车记录仪
			ComponentName comp = new ComponentName("com.android.camera2", "com.android.camera.CameraLauncher");//第一个参数是app包名，第二个是你要掉的activity的包名
			Intent intent = new Intent();
			intent.setComponent(comp);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(intent);
		}  else if (ROM_OPEN_NAVI.equals(type)) {  //打开导航//com.autonavi.xmgd.navigator
			String pkg = getDefultPackages(context);
			RunAppUtil.RunProcessMethod(context, pkg);
		}else if (ROM_CLOSE_NAVI.equals(type)) {  //关闭导航
			String pkg = getDefultPackages(context);
			packages = pkg;
			ActivityManager manager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);       
			manager.killBackgroundProcesses(pkg);  
			if(pkg.equals("com.autonavi.xmgd.navigator")){
				TimerTask task = new TimerTask() {@Override
				public void run() {
			    ActivityManager manager = (ActivityManager)
			    mContext.getSystemService(Context.ACTIVITY_SERVICE);       
				manager.killBackgroundProcesses(packages);  }};
				Timer timer = new Timer();
				timer.schedule(task, 3000);
			Intent _intent = new Intent(Intent.ACTION_MAIN);
			_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			_intent.addCategory(Intent.CATEGORY_HOME);
			context.startActivity(_intent);
			}
		} else if(ROM_OPEN_CAMERA.equals(type)){     // 抓拍照片
			Intent intent = new Intent("action.TAKE_PICTURE");
			intent.addFlags(Intent. FLAG_INCLUDE_STOPPED_PACKAGES);
			context.sendBroadcast(intent);
		} else if (ROM_CLOSE_RADIO.equals(type)) {  //关闭FM
			RunAppUtil.CloseAppMethod(context, "FM发射");
		}
	}
	public static String getDefultPackages(Context context){
		String packages = "";
		String navi = Settings.System.getString(context.getContentResolver(), NAVIGATION_KEY);
		if(TextUtils.isEmpty(navi)||"com.autonavi.xmgd.navigator".equals(navi)){
			packages = "com.autonavi.xmgd.navigator";}
		else{packages = navi;}return packages;
	}
	public static boolean isNetWorkConnected(Context context) {
		ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Activity.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = manager.getActiveNetworkInfo();
		return (networkInfo != null && networkInfo.isConnected());
	}
//	public static String getLocationNavi(Context context,String actionType){
//		String tts = "";
//		ContentResolver mContentResolver = context.getContentResolver();
//		Uri uri = NAVILocationProvider.URI;
//		String[] projection = NAVILocationProvider.PARAM;
//		String selection = String.format("navi_type ='%s'",actionType);
//		Cursor cursor = mContentResolver.query(uri, projection, selection, null, null);
//		if(cursor!=null&&cursor.getCount()>0){cursor.moveToFirst();
//		String lng = cursor.getString(cursor.getColumnIndex(NAVILocationProvider.NAVI_LNG));
//		String lat = cursor.getString(cursor.getColumnIndex(NAVILocationProvider.NAVI_LAT));
//		if(actionType.equals(GO_HOME)){tts=context.getResources().getString(R.string.voice_go_home);}
//		else{tts=context.getResources().getString(R.string.voice_go_company);}
//		StartNavi(context, lng, lat);}else{
//		if(isNetWorkConnected(context)){
//			if(actionType.equals(GO_HOME)){tts=context.getResources().getString(R.string.setting_home_navi);}
//			else{tts=context.getResources().getString(R.string.setting_company_navi);}
//			Intent intent = new Intent();
//			intent.setClass(context, PoiKeywordSearchActivity.class);
//			intent.putExtra("actionType", actionType);
//			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//			context.startActivity(intent);}else{
//			tts=context.getResources().getString(R.string.is_network_isconnected);}}return tts;}
	
	public static void StartNavi(Context context,String lng,String lat){
		if(TextUtils.isEmpty(getDefultPackages(context))||XMGD_NAVI_PACKAGE.equals(getDefultPackages(context))){
        String URI = String.format("NAVI:%s,%s",lng,lat);
		Intent intent = new Intent("com.autonavi.xmgd.action.NAVIGATOR");
        intent.setData(Uri. parse(URI));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);}
	    else{
		String URI = String.format("(TNC%s,%s)",lng,lat);
		Intent intent = new  Intent("android.NaviOne.CldStdTncReceiver");
		intent.putExtra("CLDTNC",URI);
		context.sendBroadcast(intent);}}

}
